﻿using Gl_mes.BLL.Mes;
using MesBSW.BLL.Mes;
using MesBSW.DAL.MesDB;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesBSW.View.Dialog.Mes.Materail
{
    public partial class CopyMaterialDia : Form
    {
        public CopyMaterialDia()
        {
            InitializeComponent();
        }

        #region "内部变量"
        private MaterialControler _mat_ctrl = new MaterialControler();
        private KingdeeControler _king_ctrl = new KingdeeControler();

        private List<MatGroup> _group_list = new List<MatGroup>();
        private List<MatInfo> _mat_list = new List<MatInfo>();
        #endregion

        #region "控件事件"
        /// <summary>
        /// 加载金蝶资料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoading_Click(object sender, EventArgs e)
        {
            //取得金蝶系统所有分类和物料信息
            this._group_list = this._king_ctrl.GetMaterialGroup();
            this._mat_list = this._king_ctrl.GetMaterialInfo();

            //取得MES系统中的分类，将已经在MES系统存在的分类信息从this._group_list变量中删掉
            List<MatGroup> matGroupList = this._mat_ctrl.SearchMesMaterialGroup();
            if (matGroupList != null && matGroupList.Count > 0)
            {
                var tmpList = matGroupList.Select(q => q.group_id).Distinct().ToList();
                var query = this._group_list.Where(q => tmpList.Contains(q.group_id));
                var delList = query != null && query.Count() > 0 ? query.ToList<MatGroup>() : new List<MatGroup>();
                if (delList != null && delList.Count > 0)
                {
                    foreach (var matGroup in delList)
                    {
                        this._group_list.Remove(matGroup);
                    }
                }

            }

            //取得MES系统中的物料信息，将已经在MES系统存在的物料信息从this._mat_list变量中删掉
            List<MatInfo> mesMatList = this._mat_ctrl.SearchMesMaterialInfo();
            if (mesMatList != null && mesMatList.Count > 0)
            {
                var tmpList = mesMatList.Select(x => x.mat_id).ToList();
                var query = this._mat_list.Where(q => tmpList.Contains(q.mat_id));
                var delList = query.ToList<MatInfo>();
                if (delList != null && delList.Count > 0)
                {
                    foreach (var tmpMat in delList)
                    {
                        this._mat_list.Remove(tmpMat);
                    }
                }
            }

            //显示不存在于MES系统的物料信息
            this.dgvMaterial.DataSource = this.ChangeMatInfoToDataTable(this._mat_list);
            if (this._mat_list is null || this._mat_list.Count <= 0)
            {
                UIMessageBox.ShowWarning("金蝶系统中没有新增的资料存在！");
            }
        }

        /// <summary>
        /// 关闭界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 将物料
        /// </summary>
        /// <param name="matList"></param>
        /// <returns></returns>
        private DataTable ChangeMatInfoToDataTable(List<MatInfo> matList)
        {
            DataTable resDt = new DataTable();
            resDt.Columns.Add("index", typeof(int));
            resDt.Columns.Add("mat_id", typeof(int));
            resDt.Columns.Add("mat_number", typeof(string));
            resDt.Columns.Add("mat_name", typeof(string));
            resDt.Columns.Add("mat_spe", typeof(string));
            resDt.Columns.Add("base_unit", typeof(string));
            resDt.Columns.Add("create_orgid", typeof(string));
            resDt.Columns.Add("use_orgid", typeof(string));
            resDt.Columns.Add("mat_group", typeof(int));
            resDt.Columns.Add("doc_status", typeof(string));
            resDt.Columns.Add("forbid_status", typeof(string));

            if (matList != null && matList.Count > 0)
            {
                int index = 0;
                foreach (var tmpMat in matList)
                {
                    DataRow addRow = resDt.NewRow();
                    addRow["index"] = index + 1;
                    addRow["mat_id"] = tmpMat.mat_id;
                    addRow["mat_number"] = tmpMat.mat_number;
                    addRow["mat_name"] = tmpMat.mat_name;
                    addRow["mat_spe"] = tmpMat.mat_spe;
                    addRow["base_unit"] = tmpMat.base_unit;
                    addRow["create_orgid"] = tmpMat.create_orgid.ToString();
                    addRow["use_orgid"] = tmpMat.use_ordid.ToString();
                    addRow["mat_group"] = tmpMat.mat_group;
                    addRow["doc_status"] = tmpMat.doc_status;
                    addRow["forbid_status"] = tmpMat.forbid_status;
                    resDt.Rows.Add(addRow);
                    index++;
                }
            }
            return resDt;
        }


        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this._mat_list != null && this._mat_list.Count > 0)
            {
                try
                {
                    int count = this._mat_ctrl.SaveMaterialAndGroup(this._group_list, this._mat_list);
                    UIMessageBox.ShowSuccess("物料明细信息保存成功！");
                    this.Close();
                }
                catch (Exception ex)
                {
                    UIMessageBox.ShowError("保存物料明细信息失败！" + "\r\n" + ex.Message);
                }
            }
            else
            {
                UIMessageBox.ShowWarning("没有任何可以保存的物料信息！");
            }
        }
    }
}
